package com.zte.kaoqin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.zte.kaoqin.bean.AttendanceHistory;



public class NginxPageEntry  { 

	public static void startTask(String dateStr) {
		String preReportName = "qb_nginx";
		String reportName = "qb_page_entry_nginx"; 
		dateStr = dateStr.substring(0, 8); 

		if (!TaskDBHelper.getInstance().isExcludeTask(dateStr, reportName)	&& !TaskDBHelper.getInstance().isTaskDoneOrDoing(dateStr,	reportName)
				&& TaskDBHelper.getInstance().isTaskDone(dateStr,	preReportName)) {
			try {
				System.out.println(CTime.getTime(14) + " " + reportName+ " setTaskDoing");
				TaskDBHelper.getInstance().setTaskDoing(dateStr,		reportName, CTime.getTime(14) + "Doing ");
				System.out.println(CTime.getTime(14) + " " + reportName	+ " task start");

				report(); 
				System.out.println(CTime.getTime(14) + " " + reportName			+ " task finish");
				TaskDBHelper.getInstance().setTaskDone(dateStr, reportName,			CTime.getTime(14) + "Done ");
				System.out.println(CTime.getTime(14) + " " + reportName			+ " setTaskDone");
				if (!MailUtil.send(ParamConfig.getInstance().getErrorMailReceive(), "", "qb_page_entry_nginx��ݴ������",dateStr+" "+reportName +" ok!" , "")) {
					try {
						throw new Exception("send mail error:dateStr=" + dateStr + " qb_page_entry_nginx��ݴ������");
					} catch (Exception e1) { 
						e1.printStackTrace();
					}
				}
			} catch (Exception e) { 
				TaskDBHelper.getInstance().setTaskError(dateStr,				reportName,			CTime.getTime(14) + " " + e.getMessage());
				System.out.println(CTime.getTime(14) + " " + reportName			+ " setTaskError");
				if (!MailUtil.send(ParamConfig.getInstance().getErrorMailReceive(), "", "qb_page_entry_nginx��ݴ������",dateStr+" "+Common.getErrorInfoFromException(e), "")) {
					try {
						throw new Exception("send mail error:dateStr=" + dateStr + "qb_page_entry_nginx��ݴ������");
					} catch (Exception e1) { 
						e1.printStackTrace();
					}
				}
			}
		} else {
			System.out.println(CTime.getTime(14) + " " + reportName + " isExcludeTask OR isTaskDoneOrDoing");
		}
	}


	public static void report( ) throws Exception {   
		String dateStr = CTime.getTime(14).substring(0,8);
		String beforeDate = CTime.getDate(dateStr,-1);  
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyyMMdd" );
		SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
		String week = dateFm.format(sdf.parse(beforeDate));
		System.out.println( week);

		StringBuilder sql=new StringBuilder();
		sql.setLength(0); 
		sql.append("select ID,USER_ID,ATTENCE_DATE, if(attence_status='01','01','00') STATUS from T_ATTENDANCE " );
		sql.append("where ATTENCE_DATE  ='"+beforeDate+"' ");
		System.out.println(new Date() + "Running1: " + sql.toString());
		Collection<HashMap<String, String>>	col = MysqlDBHelp.selectTable(sql.toString() ); 
		System.out.println(col.size());

		sql.setLength(0); 
		sql.append("select ID from T_USER");
		System.out.println(new Date() + "Running1: " + sql.toString());
		Collection<HashMap<String, String>>	empCol = MysqlDBHelp.selectTable(sql.toString() ); 
		System.out.println(empCol);
		List<AttendanceHistory> list = new ArrayList<AttendanceHistory>();
	//	if(!"������".equals(week) && !"������".equals(week)){
			for(HashMap<String, String> map:empCol){
				AttendanceHistory  attendanceHistory = new AttendanceHistory();
				attendanceHistory.setId(UuidUtil.getUuid());
				attendanceHistory.setAttendance_time(beforeDate );
				attendanceHistory.setStatus("00");
				attendanceHistory.setEmp_id(map.get("id"));
				for(HashMap<String, String> map1:col){
					if(map1.get("user_id").equals(map.get("id"))){
						attendanceHistory.setAttendance_time(map1.get("attence_date"));
						attendanceHistory.setStatus(map1.get("status"));
						attendanceHistory.setEmp_id(map1.get("user_id"));
						break;
					}
				}
			
				list.add(attendanceHistory);
				System.out.println(map.get("id"));
			}
//		}
			
			System.out.println(list.size());
	} 


	public static void main(String[] s) throws Exception{   
		report( );
	} 
}
